<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class CountryDetailsApiController extends AbstractRestfulController
{
    public function getList()
    {
	  	header('Access-Control-Allow-Origin: *');
		$countryTable=$this->getServiceLocator()->get('Models\Model\CountryFactory');		
		$countryWithFlaglist = $countryTable->getCountryWithFlagList()->toarray();
		if(count($countryWithFlaglist)!=0){
			return new JsonModel(array(
				'value' 	=> 1,
				'countryWithFlagList'	    => json_encode($countryWithFlaglist),
			));			
		}else{
			return new JsonModel(array(
				'value'		 => 0,
				'data'  	 => 'No Data',
			));			
		}
    }
    public function get($country)
    {
		header('Access-Control-Allow-Origin: *');		
		$countrydetailsTable=$this->getServiceLocator()->get('Models\Model\CountryFactory');				
		$countryDetails = $countrydetailsTable->countryDetails($country)->toarray();
		if(count($countryDetails)!=0){
			return new JsonModel(array(
				'value' 	=> 1,
				'countryDetails'	=> json_encode($countryDetails),
			));			
		}else{
			return new JsonModel(array(
				'value' => 0,
				'data'  => 'No Data',
			));			
		}
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');		
		$countrydetailsTable=$this->getServiceLocator()->get('Models\Model\CountryFactory');
		$getCountriesInfo = $countrydetailsTable->getCountriesInfo($data)->toarray();
		if(count($getCountriesInfo)!=0){
		$getCapitals = $countrydetailsTable->getCapitals($getCountriesInfo[0]['country_capital'])->toarray();
		$a=$getCapitals[0]['country_currency'];
		$b=$getCapitals[1]['country_currency'];
		$c=$getCapitals[2]['country_currency'];
		$d=$getCapitals[3]['country_currency'];
		$h=$getCapitals[0]['country_official_language'];
		$i=$getCapitals[1]['country_official_language'];
		$j=$getCapitals[2]['country_official_language'];
		$k=$getCapitals[3]['country_official_language'];
		if($a==$b || $a==$c || $a==$d ||$b==$c ||$b==$d ||$c==$d){
			$getCapitals = $countrydetailsTable->getCapitals($getCountriesInfo[0]['country_capital'])->toarray();
			return $this->testOptions($data,$getCountriesInfo,$getCapitals);
		}
		if($h==$i || $h==$j || $h==$k ||$i==$j ||$i==$k ||$j==$k){
			$getCapitals = $countrydetailsTable->getCapitals($getCountriesInfo[0]['country_capital'])->toarray();
			return $this->testOptions($data,$getCountriesInfo,$getCapitals);
		}
			return new JsonModel(array(
				'value' 	=> 1,
				'redd' 	=> 5,
				'capitalInfo'	=> json_encode($getCountriesInfo),
				'countryInfo'	=> json_encode($getCapitals),
			));			
		}else{
			return new JsonModel(array(
				'value' => 0,
				'data'  => 'No Data',
			));			
		}
	}
    public function update($id, $data)
    {
        
    }
    public function delete($id)
    {
       
    }
	public function testOptions($data,$getCountriesInfo,$getCapitals){
		$countrydetailsTable=$this->getServiceLocator()->get('Models\Model\CountryFactory');
		$getCapitals = $countrydetailsTable->getCapitals($getCountriesInfo[0]['country_capital'])->toarray();
		$a=$getCapitals[0]['country_currency'];
		$b=$getCapitals[1]['country_currency'];
		$c=$getCapitals[2]['country_currency'];
		$d=$getCapitals[3]['country_currency'];
		$h=$getCapitals[0]['country_official_language'];
		$i=$getCapitals[1]['country_official_language'];
		$j=$getCapitals[2]['country_official_language'];
		$k=$getCapitals[3]['country_official_language'];
		if($a==$b || $a==$c || $a==$d ||$b==$c ||$b==$d ||$c==$d){
			$getCapitals = $countrydetailsTable->getCapitals($getCountriesInfo[0]['country_capital'])->toarray();
			return $this->testOptions($data,$getCountriesInfo,$getCapitals);
		}else if($h==$i || $h==$j || $h==$k ||$i==$j ||$i==$k ||$j==$k){
			$getCapitals = $countrydetailsTable->getCapitals($getCountriesInfo[0]['country_capital'])->toarray();
			return $this->testOptions($data,$getCountriesInfo,$getCapitals);
		}
		return new JsonModel(array(
			'value' 	=> 1,
			'redd' 	=> 3,
			'capitalInfo'	=> json_encode($getCountriesInfo),
			'countryInfo'	=> json_encode($getCapitals),
		));	
	}
}